/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.model.Authority;
import org.crystalrange.service.AuthorityService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class AuthorityListController extends BaseController {

    protected AuthorityService authorityService;
    protected Authority selectedAuthority;
    protected Listbox listboxAuthority;
    protected Search searchAuthority;
    protected List<Authority> authorities;

    public AuthorityService getAuthorityService() {
        return this.authorityService;
    }

    public void setAuthorityService(AuthorityService authorityService) {
        this.authorityService = authorityService;
    }

    public Authority getSelectedAuthority() {
        return this.selectedAuthority;
    }

    public void setSelectedAuthority(Authority selectedAuthority) {
        this.selectedAuthority = selectedAuthority;
    }

    public List<Authority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchAuthority = new Search(Authority.class);
        this.searchAuthority.setMaxResults(100);
        this.searchAuthority.addFetch("createdBy");
        this.searchAuthority.addFetch("updatedBy");
        this.authorities = this.authorityService.search(this.searchAuthority);
        
        component.setAttribute("controller", this, false);
    }

    public void onClick$menuitemAuthorityCreate(Event event) throws Exception {
        this.authorityCreate();
    }

    public void onClick$menuitemAuthorityEdit(Event event) throws Exception {
        this.authorityEdit();
    }

    public void onClick$menuitemAuthorityDelete(Event event) throws Exception {
        this.authorityDelete();
    }

    public void onClick$menuitemAuthoritySearch(Event event) throws Exception {
        this.authoritySearch();
    }

    public void onClick$menuitemAuthorityRefresh(Event event) throws Exception {
        this.authorityRefresh();
    }

    public void onClick$toolbarbuttonAuthorityCreate(Event event) throws Exception {
        this.authorityCreate();
    }

    public void onClick$toolbarbuttonAuthorityEdit(Event event) throws Exception {
        this.authorityEdit();
    }

    public void onClick$toolbarbuttonAuthorityDelete(Event event) throws Exception {
        this.authorityDelete();
    }

    public void onClick$toolbarbuttonAuthoritySearch(Event event) throws Exception {
        this.authoritySearch();
    }

    public void onClick$toolbarbuttonAuthorityRefresh(Event event) throws Exception {
        this.authorityRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.authorityEdit();
    }
    
    private void authorityCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/authority.zul");
    }

    private void authorityEdit() throws Exception {
        if (this.selectedAuthority != null) {
            this.execution.sendRedirect("~./zul/html/authority.zul?Id=" + this.selectedAuthority.getId().toString());
        }
    }

    private void authorityDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxAuthority.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Authority authority = (Authority) selectedItem.getValue();
                    this.authorityService.remove(authority);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.authorities = this.authorityService.search(this.searchAuthority);
        this.binder.loadAll();
    }

    private void authoritySearch() throws Exception {
        this.execution.setAttribute("searchAuthority", this.searchAuthority);

        Window window = (Window) this.execution.createComponents("~./zul/html/authoritySearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchAuthority = (Search) this.execution.getAttribute("searchAuthority");
        }

        this.authorities = this.authorityService.search(this.searchAuthority);
        this.binder.loadAll();
    }

    private void authorityRefresh() {
        this.authorities = this.authorityService.search(this.searchAuthority);
        this.binder.loadAll();
    }
}
